package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    /**
     * 处理分页和显示数据
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo){
        //此处分页的Page对象不是之前mybatis里面的PageInfo对象
        //他是spring-data-mongodb 这个组件封装对象.
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return  JsonResult.success();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object updateInfo (Strategy strategy){
       strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state,String id){
        strategyService.changeState(state,id);
        return JsonResult.success();
    }


    @RequestMapping("/input")
    public Object input (Model model,String id){

        //catalogs攻略分类,返回的是一个集合,对象是CatalogVO
        List<CatalogVO> list = strategyCatalogService.queryCatalogs();
        //设置共享
        model.addAttribute("catalogs",list);
        //themes
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes",themes);
        //strategy
        if(StringUtils.hasLength(id)){
            Strategy strategy = strategyService.selectOne(id);
            model.addAttribute("strategy",strategy);
        }
        return "strategy/input";
    }




}